package com.dhl.fin.api.domain;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.dhl.fin.api.common.domain.BasicDomain;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.constant.RemindConstant;
import com.dhl.fin.api.dto.NextTimeDTO;
import com.dhl.fin.api.util.CronJobUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.sql.Timestamp;

/**
 * @author CuiJianbo
 * @since 2025/5/6
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "t_extra_job_cron")
public class ExtraJobCron extends BasicDomain {

    public ExtraJobCron() {
    }

    public ExtraJobCron(String scheduleId, Timestamp executeTime, Timestamp targetTime, RemindTask remindTask, String remark) {
        this.scheduleIdNum = scheduleId;
        this.cron = CronJobUtil.dateToCron(executeTime);
        this.executeTime = executeTime;
        this.remindTask = remindTask;
        this.remark = remark;
        this.targetTime = targetTime;
    }

    public ExtraJobCron(String scheduleId, NextTimeDTO nextTimeDTO, MensesRemindTask mensesRemindTask) {
        Timestamp targetTime = DateUtil.parseDateTime(nextTimeDTO.getNextTime()).toTimestamp();
        Timestamp executeTime = DateUtil.parseDateTime(nextTimeDTO.getNextTime()).toTimestamp();
        String cronExpress = CronJobUtil.dateToCron(executeTime);
        this.scheduleIdNum = scheduleId;
        this.cron = cronExpress;
        this.executeTime = executeTime;
        this.mensesRemindTask = mensesRemindTask;
        this.remark = nextTimeDTO.getRemark();
        this.targetTime = targetTime;
    }

    private String scheduleIdNum;

    private String cron;

    private Timestamp executeTime;

    private Timestamp targetTime;

    private String remark;

    @ManyToOne
    @JoinColumn(name = "remind_task_id")
    private RemindTask remindTask;

    @ManyToOne
    @JoinColumn(name = "menses_remind_task_id")
    private MensesRemindTask mensesRemindTask;


    public NextTimeDTO changeToNextTimeDTO() {
        return NextTimeDTO.changeToNextTime(executeTime, targetTime, remark);
    }

    public String getRemainTime() {
        //TODO 使用当前时间计算时间差
        String remindTime = "";
        long hours = DateUtil.between(executeTime, targetTime, DateUnit.HOUR);
        if (hours == 0) {
            long minutes = DateUtil.between(executeTime, targetTime, DateUnit.MINUTE);
            if (minutes == 0) {
                long second = DateUtil.between(executeTime, targetTime, DateUnit.SECOND);
                remindTime = second + RemindConstant.TIME_UNIT_CN_NAME_SECOND;
            } else {
                remindTime = minutes + RemindConstant.TIME_UNIT_CN_NAME_MINUTES;
            }
        } else if (hours < 24) {
            remindTime = hours + RemindConstant.TIME_UNIT_CN_NAME_HOUR;
        } else {
            remindTime = (hours / 24) + RemindConstant.TIME_UNIT_CN_NAME_DAY;
        }
        if (DateUtil.formatDateTime(executeTime).equals(DateUtil.formatDateTime(targetTime))) {
            return "0天";
        } else {
            return ObjectUtil.notNull(targetTime) && executeTime.before(targetTime) ? remindTime : "-" + remindTime;
        }
    }

}
